package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class checkafter
 */
@WebServlet("/checkafter")
public class checkafter extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public checkafter() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		String classtime = request.getParameter("classtime");// 获取当日课时
		String classname = request.getParameter("classname");
		String centername = request.getParameter("centername");
		String checktable = centername + classname + "check";// 获得将要写入的考勤表格名称
		String check_date = request.getParameter("checkdate");// 获取考勤日期
		//String find_checkdate = check_date + "未出勤";// 用于在check_info中查找日期的时间变量
		String[] b = check_date.split("-");// 并且将2018-01-13转换为2018年01月13号
		System.out.println(Arrays.toString(b));

		String oldc = b[0] + "年" + b[1] + "月" + b[2] + "日";// 当天签到表格
		// C为要插入的属性名称，下面还要用

		// 获取从前台一起传过来的对象，并且遍历获取
		String stuid = request.getParameter("stuid");
		String stuname = request.getParameter("stuname");
		String check = request.getParameter("checkaftertime");

		// 此时还要将contract表中的课时数减去
		if (check.equals("1")) {
			if (classtime.equals("1")) {
				String cutclass = "update contract set restclass = restclass-1 where id='" + stuid + "' ";
				try {
					Jdbc cut1 = new Jdbc(cutclass);
				} catch (ClassNotFoundException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (classtime.equals("1.5")) {
				String cutclass = "update contract set restclass = restclass-1.5 where id='" + stuid + "' ";
				try {
					Jdbc cut2 = new Jdbc(cutclass);
				} catch (ClassNotFoundException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			// 将考勤登记到班级考勤表中去
			String update1 = "update " + checktable + " set " + oldc + "='2' where stuid='" + stuid + "'";
			String updateabsence="update "+checktable+" set absence=absence+1 where stuid='"+stuid+"'";
			//String update2 = "update check_info set absence = absence-1 where stuid='" + stuid + "'";
			// 并且将check_info表中的值也更新
			// 但是得找到在checkinfo表中的相应的对上的签到日期
			//String select = "select * from check_info where stuid='" + stuid + "'";
//			try {
//				Jdbc sci = new Jdbc();
//				sci.Jdbc_Q2(select);
//				sci.rs.first();// 结果集
//				String checkdate;
//				for (int i = 1; i <= 90; i++) {
//					checkdate = sci.rs.getString("check" + i);
//					System.out.println(checkdate);
//					if (checkdate != null && checkdate.equals(find_checkdate)) {
//						String uci = "update check_info set check" + i + "='" + check_date + "已补' where stuid='" + stuid
//								+ "'";
//						Jdbc j1 = new Jdbc(uci);
//						break;
//					}
//
//				}
//			} catch (ClassNotFoundException | SQLException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}

			// 将当天考勤的
			try {
				Jdbc j3 = new Jdbc(update1);
				Jdbc j4 = new Jdbc(updateabsence);
			} catch (ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		request.getRequestDispatcher("check_after.jsp?checkdate=" + check_date + "&classname=" + classname
				+ "&centername=" + centername + "").forward(request, response);

	}

}
